/*
 * Class:CEMCalendar
 * 
 * Description: This file describes a Calendar, which will contain the agendas of the user.
 * 
 * Version: 1.0 - Nikolay Neshev, Sonny Guï¿½nette - 29-10-2011
 * 		Initial implementation
 * Version : 1.1 - F-X Frenette - 2011-12-09
 * 		Modify agenda list management functions
 * 		Modify comments for JAVADOC
 * 
 * Copyright GPP - Genuine People Personality inc
 * 
 */

package calendarEventManager;


import java.util.ArrayList;
import java.util.List;


public class CEMCalendar implements CEMConstants {
	
	//**************************************************************
	// MODAL VARIABLES
	//**************************************************************	
	/** A list of the calendar's agenda*/
	private static CEMCalendar s_oInstance = null;
	private List<CEMAgenda> m_oAgendaList;

	//**************************************************************
	// Method: CEMCalendar
	// Description: Class constructor
	//**************************************************************
	/**
	 *  
	 */
	private CEMCalendar(){
		m_oAgendaList = new ArrayList<CEMAgenda>();
	} // CEMCalendar()
	
	//**************************************************************
	// Method: getInstance
	// Description: This method makes sure that there is only one
	//              instance of CEMCalendar hence respecting
	//              the Singleton design pattern.
	//**************************************************************
	/**
	 * @return CEMCalendar
	 *		The instance
	 */
	public synchronized static CEMCalendar getInstance(){
		if(s_oInstance == null){
			s_oInstance = new CEMCalendar();
		}
		return s_oInstance;
	} // getInstance()
	
	//**************************************************************
	// Method: addAgenda
	// Description: Adds a new agenda to the agenda list
	//**************************************************************
	/**
	 * @param p_oAgenda
	 * 		Agenda to be added
	 */
	public void addAgenda(CEMAgenda p_oAgenda){
			m_oAgendaList.add(p_oAgenda);
	} // addAgenda()	
	
	//**************************************************************
	// Method: getAgendaList
	// Description: Returns the current list of agendas
	//**************************************************************
	/**
	 * @return List<CEMAgenda>
	 * 		List of agendas
	 */
	public List<CEMAgenda> getAgendaList(){
		return m_oAgendaList;
	} // getAgendaList()

	//**************************************************************
	// Method: getAgenda
	// Description: Get a single agenda
	//**************************************************************
	/**
	 * @param p_nAgendaId
	 * 		Agenda's Id
	 * @return CEMAgenda
	 * 		Agenda requested
	 */
	public CEMAgenda getAgenda(int p_nAgendaId){
		CEMAgenda oAgenda = null;
		
		for(CEMAgenda oWork : m_oAgendaList){
			if(oWork.getAgendaId() == p_nAgendaId){
				oAgenda = oWork;
				break;
			}
		}
		
		return oAgenda;
	} // getAgenda()

	//**************************************************************
	// Method: deleteAgenda
	// Description: Deletes the agenda from the agenda list
	//**************************************************************
	/**
	 * @param p_nAgendaId
	 * 		Agenda's Id
	 * @return int
	 * 		Return Code (See CEMConstants for specifications on possible return codes)
	 */
	public int deleteAgenda(int p_nAgendaId){
		int nRc = CEM_RETURN_CODE_ERROR;
		
		for(CEMAgenda oWork : m_oAgendaList){
			if(oWork.getAgendaId() == p_nAgendaId){
				//delete from the list
				m_oAgendaList.remove(oWork);
				nRc = CEM_RETURN_CODE_OK;
				break;
			}
		}
		return nRc;
	} // deleteAgenda()	

	//**************************************************************
	// Method: loadAgendas
	// Description: Loads the agendas
	//**************************************************************
	/**
	 * @param p_oAgendas
	 * 		List of agendas to be loaded
	 */
	public void loadAgendas(List<CEMAgenda> p_oAgendas){
		m_oAgendaList = p_oAgendas;
	} // loadAgendas()
	
	//**************************************************************
	// Method: loadAgendas
	// Description: Returns the agenda Id
	//**************************************************************
	/**
	 * @param p_sAgendaName
	 * 		The agenda's name
	 * @return int
	 * 		The agenda's Id
	 */
	public int getAgendaId(String p_sAgendaName){
		int agendaId = -1;
		for(CEMAgenda oWork : m_oAgendaList){
			if(oWork.getAgendaName() == p_sAgendaName){
				agendaId = oWork.getAgendaId();
				break;
			}
		}
		
		return agendaId;
	} // getAgendaId()
	
} // CEMCalendar
